var express = require('express'),
    swig = require('swig-templates'),
    request = require('request'),
    router = express.Router(),
    common = require('../base/common.js');
router.get('/',function(req, res,next) {
    var thatData = {
        thatData: {}
    }
    try {
        // if (req.query.pp) {
        //     res.cookie('pp', req.query.pp, {maxAge: 1000*60*60*24*14, httpOnly: false});
        // }
        // if (req.query.cp) {
        //     res.cookie('cp', req.query.cp, {maxAge: 1000*60*60*24*14, httpOnly: false});
        // }
        if (req.query.xh) {
            res.cookie('xh', req.query.xh, {maxAge: 1000*60*60*24*14, httpOnly: false});
        }
        // if (req.query.dt) {
        //     res.cookie('dt', req.query.dt, {maxAge: 1000*60*60*24*14, httpOnly: false});
        // }
        if (req.query.tm) {
            req.query.tm=req.query.tm.split(',')[0];
            res.cookie('tm', req.query.tm, {maxAge: 1000*60*60*24*14, httpOnly: false});
        }
        var tm = req.query.tm?req.query.tm:req.cookies.tm;
        for(var i in req.query){
            thatData.thatData[i]=req.query[i];
        }
        thatData.thatData.tm=tm;
        // thatData.thatData.pp=req.query.pp?req.query.pp:req.cookies.pp;
        thatData.thatData.xh=req.query.xh?req.query.xh:req.cookies.xh;
        request.get({
            url: config.resourcePath.apiRoot + "/index.php?m=Index&a=ProductIndex&oidContent="+tm
        }, function (error, response, body) {
            try {
                var result = JSON.parse(body);
                // console.log(result);
                for(var i in result){
                    thatData.thatData[i] = result[i];
                }
                if (result.p_id) {
                    res.cookie('p_id', result.p_id, {maxAge: 1000*60*60*24*14, httpOnly: false});
                }
                if (result.BarCode) {
                    res.cookie('BarCode', result.BarCode, {maxAge: 1000*60*60*24*14, httpOnly: false});
                }
                if (result.p_image) {
                    res.cookie('p_image', result.p_image, {maxAge: 1000*60*60*24*14, httpOnly: false});
                }
                if (result.mainProductId) {
                    res.cookie('mainProductId', result.mainProductId, {maxAge: 1000*60*60*24*14, httpOnly: false});
                }
                if (result.p_trait) {
                    res.cookie('p_trait', result.p_trait, {maxAge: 1000*60*60*24*14, httpOnly: false});
                }
                if (result.p_brand) {
                    res.cookie('p_brand', result.p_brand, {maxAge: 1000*60*60*24*14, httpOnly: false});
                }
                if (result.p_type) {
                    res.cookie('p_type', result.p_type, {maxAge: 1000*60*60*24*14, httpOnly: false});
                }
                if (result.p_group_name) {
                    res.cookie('p_group_name', result.p_group_name, {maxAge: 1000*60*60*24*14, httpOnly: false});
                }
                if (result.selling_point) {
                    res.cookie('selling_point', result.selling_point, {maxAge: 1000*60*60*24*14, httpOnly: false});
                }
                if (result.brandId) {
                    res.cookie('brandId', result.brandId, {maxAge: 1000*60*60*24*14, httpOnly: false});
                }
                if (result.p_mid_c) {
                    res.cookie('p_mid_c', result.p_mid_c, {maxAge: 1000*60*60*24*14, httpOnly: false});
                }
                if (result.info) {
                    res.cookie('info', result.info, {maxAge: 1000*60*60*24*14, httpOnly: false});
                }
            } catch (e) {
            }
            res.render('my-haier/new_yj', thatData);
        });
    } catch (e) {
        res.render('my-haier/new_yj', thatData);
    }
});
router.get('/my-haier1l',function(req, res,next) {
    // 数据请求接口
    res.render('my-haier/My-Haier_1l',{});

    //request.get({
    //    url:config.resourcePath.apiRoot+"/index.php?m=Index&a=index"
    //}, function (error, response, body) {
    //    var result = JSON.parse(body);
    //    if(result.Code==200){
    //        var renderData = {
    //                thatData: result
    //            };
    //            console.log();
    //        // 将renderData参数返回到页面
    //        res.render('my-haier/My-Haier_1l',renderData);
    //    }
    //});
});
router.get('/my-product-1',function(req, res,next) {
    res.render('my-haier/my_product/My_Product_1');
});
router.get('/online-service',function(req, res,next) {
    res.render('my-haier/online_service/online_service');
});
router.get('/service-schedule',function(req, res,next) {
    res.render('my-haier/service_schedule/service_schedule');
});
router.get('/vip-permission',function(req, res,next) {
    res.render('my-haier/vip_permission/vip_permission');
});
router.get('/electronicmanual',function(req, res,next) {
    res.render('my-haier/electronicmanual/electronicmanual');
});
router.get('/seashells',function(req, res,next) {
    res.render('my-haier/seashells/seashells');
});
router.get('/ndustry_rights',function(req, res,next) {
    res.render('my-haier/ndustry_rights/ndustry_rights');
});
router.get('/clear-middle',function(req, res,next) {
    res.render('my-haier/clear_middle/clear_middle');
});
module.exports = router;
